package com.tianqu.tms.www.bid.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucCarInfo;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.www.bid.action.BidCarSerchAllListAction;
import com.tianqu.tms.www.bid.actionform.BidCarSerchAllForm;
import com.tianqu.tms.www.bid.viewhelper.BidCarSerchInputViewHelper;

public class BidCarSerchAllListController extends TmsController {

	private Logger logger = Logger.getLogger(BidCarSerchAllListController.class);
	public static final String PAGING = "paging";

	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Codes codes = Codes.getInstance();
			BidCarSerchAllForm carForm = (BidCarSerchAllForm) uc.getInputForm();
			AucCarInfo auc = AucCarInfo.getInstance();

			if (uc.getAttribute(BidCarSerchAllListAction.PAGENO) != null) {
				auc.setPageNo(Integer.parseInt((String) uc
						.getAttribute(BidCarSerchAllListAction.PAGENO)));
			}
			if (uc.getAttribute(BidCarSerchAllListAction.MAXINDEX) != null) {
				auc.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(BidCarSerchAllListAction.MAXINDEX)));
			}
			if (uc.getAttribute(BidCarSerchAllListAction.SORTKEY) != null) {
				auc.setSortKey(Integer.parseInt((String) uc
						.getAttribute(BidCarSerchAllListAction.SORTKEY)));
			}
			if (uc.getAttribute(BidCarSerchAllListAction.ASC) != null) {
				auc.setAsc((new Boolean((String) uc
						.getAttribute(BidCarSerchAllListAction.ASC)))
						.booleanValue());
			}

			List<AucCarInfo> aucList = new ArrayList<AucCarInfo>();
			if (uc.getAttribute("seniorForm") == null) {
				// 判定翻页
				aucList = auc.getaucCarList((String)uc.getAttribute("searchLicensePlate"),
						(String)uc.getAttribute("searchVin"), (String)uc.getAttribute("searchStartTime"));
			} else {
				carForm = (BidCarSerchAllForm) uc.getAttribute("seniorForm");
				aucList = auc.getaucCarList(carForm.getLicensePlate(), carForm
						.getVin(), carForm.getStartTime());
			}

			BidCarSerchInputViewHelper viewHelp = new BidCarSerchInputViewHelper();
			List<BidCarSerchInputViewHelper> view = view(aucList, codes, auc);

			viewHelp.setList(view);

			uc.setAttribute(TmsConstants.VIEWHELP, viewHelp);
			uc.setAttribute("seniorForm", carForm);
			uc.setAttribute(PAGING, auc);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

	/**
	 * 获得viewList
	 */
	private List<BidCarSerchInputViewHelper> view(List<AucCarInfo> aucList,
			Codes codes, AucCarInfo auc) {
		List<BidCarSerchInputViewHelper> viewList = new ArrayList<BidCarSerchInputViewHelper>();
		int wp = 0;
		if (auc.getPageNo() == 1) {
			wp = 0;
		} else {
			wp = (auc.getPageNo() - 1) * 10;
		}
		if (aucList != null && aucList.size() > 0) {
			for (int i = 0; i < aucList.size(); i++) {
				BidCarSerchInputViewHelper vie = new BidCarSerchInputViewHelper();
				AucCarInfo aucCar = aucList.get(i);
				vie.setId(aucCar.getId());
				vie.setCarNum(String.valueOf((wp + i + 1)));
				vie.setSerial(aucCar.getSerial());
				vie.setCarName(aucCar.getCarName());
				vie.setLicensePlate(aucCar.getLicensePlate());
				vie.setVin(aucCar.getVin());
				if (aucCar.getNowPrice() > 0) {
					vie.setNowPrice(StringUtil.transYtoW(aucCar.getNowPrice()));
				} else {
					vie.setNowPrice("-");
				}
				if (i % 2 == 0) {
					vie.setCssFlag(1);
				} else {
					vie.setCssFlag(2);
				}
				vie.setStartTime(DateUtil.getTimeByTimeStamp1(aucCar.getStartTime()));//竞价开始时间表示更改 modify by mufeiyu
				vie.setBidStatus(StringUtil.getBidStatus(aucCar.getBidStatus()));
				viewList.add(vie);
			}
		}
		return viewList;
	}
}
